﻿define(function (require) {
    var service = require('ezdesk/utility/service');

    $(function () {
        beginLogin();

        function beginLogin() {
            $('#loginpanel').hide();
            $('#txt-userName').html("用户： " + name);
            $('#logining').show();

            service.Login(function (statusInfo) {
                if (statusInfo.Status == 'Success') {
                    $.postMessage('login:1', '*', parent);
                }
                else {
                    $('#loginpanel').show();
                    $('#logining').hide();
                }
            }, { userName: 'hg', password: '123456' });

        }

        var submitLogin = function () {
            var name = $('#txt-name').val();
            var password = $('#txt-password').val();

            $('#loginpanel').hide();
            $('#txt-userName').html("用户： " + name);
            $('#logining').show();

            service.Login(function (statusInfo) {
                if (statusInfo.Status == 'Success') {
                    alert('登陆成功！');
                    $.postMessage('login:1', '*', parent);
                }
                else {
                    alert('登录失败！')
                    $('#loginpanel').show();
                    $('#logining').hide();
                }
            }, { userName: name, password: password });
        };

        $('#txt-name').keydown(function (e) {
            if (e.keyCode == 13) {
                $('#txt-password').focus();
            }
        });

        $('#txt-password').keydown(function (e) {
            if (e.keyCode == 13) {
                submitLogin();
            }
        });

        $('#btn-login').click(function () {
            submitLogin();
        });


    })

});